﻿using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Xml.Linq;

namespace WebkitIISHost
{
    public class SystemConfig
    {
        public static string  AppPath { get; set; }
        public static int AppPort { get; set; }
        public static string VirtRoot { get; set; }
        public static string DefaultUrl { get; set; }
        public static string LogConfigPath { get; set; }

        public static string ServicesPath = "Data";

        static SystemConfig()
        {
            XElement doc = XElement.Load(Application.StartupPath + "\\Config\\SystemConfig.xml");

            LogConfigPath = Application.StartupPath + "\\Config\\log4net.config";

            var element = doc.Element("WebSite");
            if (element != null)
            {
                var ele = element.Element("AppPath");
                if (ele != null)
                    AppPath = ele.Value;
                ele = element.Element("AppPort");
                if (ele != null)
                    AppPort = int.Parse(ele.Value);
                ele = element.Element("VirtRoot");
                if (ele != null)
                    VirtRoot = ele.Value;

                DefaultUrl = string.Format(" http://localhost:{0}/DrillingDepth/depth.html", AppPort);
                ele = element.Element("DefaultUrl");
                if (ele != null)
                    DefaultUrl = ele.Value;
            }

            string RegexString = @"[a-zA-Z]:\\.*";
            var ma = Regex.Match(AppPath, RegexString);
            if (ma.Success)
            {
                ServicesPath = Path.Combine( "Data");
            }
            else
            {
                ServicesPath = Path.Combine(Application.StartupPath,  "Data");
            }
        }

        public static void Save()
        {
            var doc = new XElement("WebkitIISHost"
                , new XElement("WebSite"
                    , new XElement("AppPath", AppPath)
                    , new XElement("AppPort", AppPort)
                    , new XElement("VirtRoot", VirtRoot)
                    , new XElement("DefaultUrl", DefaultUrl)
                    ));

            doc.Save(Application.StartupPath + "\\Config\\SystemConfig.xml");
        }

      

        public static void WriteLine(string path, string drillIndex, List<string> contents)
        {
            var fullPath = Path.Combine(ServicesPath, path);

            if (!System.IO.Directory.Exists(fullPath))
                System.IO.Directory.CreateDirectory(fullPath);

            string strPath = Path.Combine(fullPath, drillIndex + ".txt");
            using (var sw = new StreamWriter(strPath, false, Encoding.GetEncoding("gb2312")))
            {
                foreach (var content in contents)
                {
                    sw.Write(content);
                    sw.WriteLine();
                }
                
                sw.Close();
            }
        }

        public static void DeleteLine(string str_drill_index)
        {
            if (!System.IO.Directory.Exists(ServicesPath))
                System.IO.Directory.CreateDirectory(ServicesPath);

            string strPath = ServicesPath + str_drill_index + ".txt";

            if (File.Exists(strPath))
                File.Delete(strPath);
        }
    }
}